/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-06-26
 * Time: 19:55
 */
public class test5 {
    public int trap(int[] height) {
        int n = height.length;
        int maxHeight = 0;
        int maxHeightIndex = -1;
        for(int i=0;i<n;i++){
            if(height[i]>maxHeight){
                maxHeight = height[i];
                maxHeightIndex = i;
            }
        }
        if(maxHeightIndex==-1){
            return 0;
        }
        // 记录结果
        int sumWater = 0;
        // 当前的位置
        int highHeight = 0;
        for(int i=0;i<maxHeightIndex;i++){
            highHeight = Math.max(highHeight,height[i]);
            sumWater += highHeight-height[i];
        }
        highHeight = 0;
        for(int i=n-1;i>maxHeightIndex;i--){
            highHeight = Math.max(highHeight,height[i]);
            sumWater += highHeight-height[i];
        }
        return sumWater;
    }
}
